<?php
/**
 * SystemEmailController is the default controller for this application
 */
class SystemEmailController extends ControllerBase {
	/**
	 * SystemEmailDataService object
	 * 
	 * @var SystemEmailDataService object
	 */
	public $systemEmailDataService = null;
	
	public function __construct() {
		parent::__construct ();
		
		// Set page name
		$this->setPageName ( 'dashboard' );
		
		$this->systemEmailDataService = new SystemEmailDataService ();
		
		$this->smarty->assign ( 'arrSystemEmailStatus', SystemEmailEntity::getStatusList () );
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 */
	public function indexAction() {
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			$arrId = $this->getPost ( 'id', null );
			$arrSubject = $this->getPost ( 'subject', null );
			$arrMessage = $this->getPost ( 'message', null );
			$arrStatus = $this->getPost ( 'status', null );
			
			if (! is_null ( $arrId ) && is_array ( $arrId ) && count ( $arrId ) > 0) {
				for($i = 0; $i < count ( $arrId ); $i ++) {
					try {
						$systemEmailEntity = $this->systemEmailDataService->beforeUpdate ( $arrId [$i] );
						$systemEmailEntity->subject = $arrSubject [$i];
						$systemEmailEntity->message = $arrMessage [$i];
						// $systemEmailEntity->message = $arrMessage[$i];
						$this->systemEmailDataService->update ( $systemEmailEntity );
					} catch ( Exception $ex ) {
						// Ignore this case
					}
				}
			}
		}
		$this->smarty->assign ( 'arrSystemEmailEntity', $this->systemEmailDataService->getItems ( array (), 'createdDate DESC' ) );
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Admin/SystemEmail/edit.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Admin/MainLayout.tpl' );
	}

}
